<template lang="jade">
div.return-bar
  i.iconfont(@click="back()") &#xe668;
  充值
div.recharge.detail_top
  header
    div.pic
      img(src="../../assets/images/user/recharge.png")
  section
    div.box.clearfix
      img.logo(src="../../assets/images/user/huaxingbank-logo.png")
      div.info
        strong 华兴银行账户
        span {{info.bank_acname}}
        span {{info.bank_acno}}
        span {{info.bank_name}}

    div.post
      div.set-money
        span 充值金额:
        input(placeholder="请输入金额",autofocus,autocomplete="off",type="number",v-model="setMoney",name="money")
      div.money
        span 充值后，账户可用余额 {{(parseInt(setMoney) || 0) +initMoney}}
      button.btn(type="button", @click="doIt()", style="margin-bottom: 0.6rem;") 立即充值
      //a.bindingCard( @click="bCard()" )  如未绑卡请点击这里

    //dl.note
      dd
        b E账户充值步骤说明(实时到帐):
          span.fl
            a 图文说明>
        div.img
          img(src="../../assets/images/user/recharge-flow1.png")
      dd
        b E账户充值步骤说明(T+1到帐):
          span.fl
            a 图文说明>
        div.img
          img(src="../../assets/images/user/recharge-flow2.png")

    dl.note
      dt
        b 华兴银行充值指引：
        div 1.充值需先绑卡激活华兴账户；
          a.bindingCard( @click="bCard()" )
            去绑定银行卡。
        div 2.有喜财富提供两种充值方式：
      dd.first 方式一：转账充值(资金实时到账)通过本人银行卡转账到华兴银行账户
      dd.second 方式二：银行卡充值(T+1到账)登录有喜财富进行充值，下一步选择“绑定银行卡”充值。
      dd 充值全程免费，最低充值金额1元。
      dd 如有疑问请联系客服0755-83167199


formpage(:url.sync="post_url", :rdata.sync="post_data")

</template>



<script>
import '../../assets/sass/user/recharge.scss'
import {Api_Bind_Card,Api_Bank_Info,Api_Recharge} from '../../config/api'
import FormPage from '../../components/common/form'

export default {

  data () {
    return {
      post_url: '',
      post_data:{},
      busy:false,
      setMoney : '',
      info: {},
      initMoney: 0,
      subForm:null
    }
  },
  components:{
    formpage:FormPage
  },
  methods: {
    getForm(){
      for(var a in this.$children){
        if(this.$children[a].name == 'form')
          this.subForm = this.$children[a];
      }
    },

    back(){
      //window.history.back();
      this.$route.router.go('/user')
    },

    bCard(){

      if(this.busy){
        return false;
      }

      this.$store.dispatch('setShowLoading', true);
      this.busy = true;

      this.$http({url:Api_Bind_Card, method:'GET'}).then(function(response){
        var rs = response.data;
        var status = rs.status, msg = rs.msg;

        if(status){

            /*this.$children[0].show = true
            this.post_url = rs.action_url
            this.post_data.RequestData = rs.RequestData
            this.post_data.transCode = rs.transCode
            this.$children[0].submit();*/
            this.getForm();
            this.subForm.show = true
            this.post_url = rs.action_url
            this.post_data.RequestData = rs.RequestData
            this.post_data.transCode = rs.transCode
            this.subForm.submit();

        }
        else{
          if(msg == undefined){
            //window.location.href = Api_Bind_Card+'&r_key='+rkey+'&p_key='+pkey;
            /*this.$children[0].show = true
            this.post_url = rs.action_url
            this.post_data.RequestData = rs.RequestData
            this.post_data.transCode = rs.transCode
            this.$children[0].submit();*/
            this.getForm();
            this.subForm.show = true
            this.post_url = rs.action_url
            this.post_data.RequestData = rs.RequestData
            this.post_data.transCode = rs.transCode
            this.subForm.submit();

          }
          else{
            alert(msg);
          }
        }

        this.busy = false;

      });

    },


    fetch: function (next) {
      this.$http({url: Api_Bank_Info, method: 'GET'}).then(function (response) {

        /*if(response.data.status == false || sessionStorage.getItem("login")==null){
          //alert(response.data.msg)
          this.$route.router.go('/login?from='+this.$route.path.substring(1))
          return false;
        }*/


        var obj = response.data;
        if(!obj.status){
          alert(obj.msg)
          if(obj.code == '10055'){
            sessionStorage.removeItem("login");
            this.$route.router.go('/login?from=recharge')
          }
          if(obj.code == '10057'){
            this.$router.go('/openAccount')
          }
        }else{
          this.info = obj.data
          this.initMoney = parseFloat(obj.data.balance)
        }

        next()
      }, function (response) {
          console.log(response)
      });
    },

    doIt: function () {
      if(this.setMoney == ''){
        alert('请输入金额！')
        return false;
      }
      if(this.busy){
        return false;
      }
      this.$store.dispatch('setShowLoading', true);
      this.busy = true;
      var data = {'money':this.setMoney}

      this.$http.post(Api_Recharge, data,'').then(function (response) {
          var rs = response.data;
          this.$store.dispatch('setShowLoading', false);

          if(!rs.status){
            alert(rs.msg)
            this.busy = false;
          }else {

/*            this.$children[0].show = true
            this.post_url = rs.action_url
            this.post_data.RequestData = rs.RequestData
            this.post_data.transCode = rs.transCode
            this.$children[0].submit();*/
            this.getForm();
            this.subForm.show = true
            this.post_url = rs.action_url
            this.post_data.RequestData = rs.RequestData
            this.post_data.transCode = rs.transCode
            this.subForm.submit();


          }
      }, function (response) {

      });



    }
  },
  route: {
    activate ( transition ) {
      var vm = this;
      this.fetch( function() {
        transition.next();

        var r_type = transition.to.query.type
        if( r_type != undefined ){
          vm.bCard();
        }
        // /recharge?type=bind
      })
    }
  }
}
</script>
